/*
 * @Author: mikey.zhaopeng 
 * @Date: 2018-11-18 11:37:00 
 * @Last Modified by: mikey.zhaopeng
 * @Last Modified time: 2018-12-13 22:49:38
 */
import 'package:flutter/material.dart';
import '../constants.dart';
import './conversation_page.dart';
import './contacts_page.dart';
import './discover_page.dart';

enum ActionItems { GROUD_CHAT, ADD_FIREND, QR_SCAN, PAYMENT }

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => new _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  PageController _pageController;
  List<NavigationIconView> _navigationViews;
  List<Widget> _pages;
  int _currentIndex = 0;
  @override
  void initState() {
    super.initState();
    _navigationViews = <NavigationIconView>[
      NavigationIconView(
        title: "微信",
        icon: IconData(0xe608, fontFamily: Constants.IconFontFamily),
        activeIcon: IconData(0xe603, fontFamily: Constants.IconFontFamily),
      ),
      NavigationIconView(
        title: "通讯录",
        icon: IconData(0xe601, fontFamily: Constants.IconFontFamily),
        activeIcon: IconData(0xe656, fontFamily: Constants.IconFontFamily),
      ),
      NavigationIconView(
        title: "发现",
        icon: IconData(0xe600, fontFamily: Constants.IconFontFamily),
        activeIcon: IconData(0xe671, fontFamily: Constants.IconFontFamily),
      ),
      NavigationIconView(
        title: "我",
        icon: IconData(0xe6c0, fontFamily: Constants.IconFontFamily),
        activeIcon: IconData(0xe626, fontFamily: Constants.IconFontFamily),
      ),
    ];
    _pageController = new PageController(initialPage: _currentIndex);
    _pages = [
      ConversationPage(),
      ContactPage(),
      // Container(color: Colors.yellow),
      DiscoverPage(),
      Container(color: Colors.green),
    ];
  }

  _buildPopuMenuItem(int iconName, String title) {
    return Row(
      children: <Widget>[
        Icon(
          IconData(
            iconName,
            fontFamily: Constants.IconFontFamily,
          ),
          color: const Color(AppColors.AppBarPopuMenuTextColor),
        ),
        Container(
          width: 16.0,
        ),
        Text(
          title,
          style:
              TextStyle(color: const Color(AppColors.AppBarPopuMenuTextColor)),
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    final BottomNavigationBar botNavBar = new BottomNavigationBar(
      fixedColor: const Color(AppColors.TabIconActive),
      items: _navigationViews
          .map((NavigationIconView navigatonView) => navigatonView.item)
          .toList(),
      currentIndex: _currentIndex,
      type: BottomNavigationBarType.fixed,
      onTap: (int index) {
        setState(() {
          _currentIndex = index;
          _pageController.animateToPage(_currentIndex,
              duration: Duration(milliseconds: 1), curve: Curves.easeInOut);
        });
      },
    );

    return Scaffold(
      appBar: AppBar(
        elevation: 0.0,
        centerTitle: false,
        title: Text(
          "微信",
        ),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.search),
            onPressed: () {
              print('点击了搜索按钮');
            },
          ),
          PopupMenuButton(
            offset: Offset(-6.0, 54.0),
            itemBuilder: (BuildContext context) {
              return <PopupMenuItem<String>>[
                PopupMenuItem(
                  child: _buildPopuMenuItem(0xe69e, "发起群聊"),
                  value: "group_chat",
                ),
                PopupMenuItem(
                  child: _buildPopuMenuItem(0xe638, "添加朋友"),
                  value: "add_firend",
                ),
                PopupMenuItem(
                  child: _buildPopuMenuItem(0xe61b, "扫一扫"),
                  value: "qr_scan",
                ),
                PopupMenuItem(
                  child: _buildPopuMenuItem(0xe62a, "收付款"),
                  value: "payment",
                ),
              ];
            },
            icon: Icon(IconData(0xe658, fontFamily: Constants.IconFontFamily)),
            onSelected: (String selectedValue) {
              print(selectedValue);
            },
          ),
        ],
      ),
      body: PageView.builder(
        itemBuilder: (BuildContext context, int index) {
          return _pages[index];
        },
        controller: _pageController,
        itemCount: _pages.length,
        onPageChanged: (int index) {
          print("当前显示的是${index}");
          setState(() {
            _currentIndex = index;
          });
        },
      ),
      bottomNavigationBar: botNavBar,
    );
  }
}

class NavigationIconView {
  final String _title;
  final BottomNavigationBarItem item;

  NavigationIconView(
      {Key key, String title, IconData icon, IconData activeIcon})
      : _title = title,
        item = new BottomNavigationBarItem(
          icon: Icon(
            icon,
          ),
          activeIcon: Icon(
            activeIcon,
          ),
          title: Text(
            title,
          ),
          backgroundColor: Colors.white,
        );
}
